package com.cy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 启动类本质上也是一个配置类，
 * 在配置类启动时会读取spring.factories配置文件，进行springboot初始配置
 */
//@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class NacosConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class, args);
    }

    @Bean

    //定义一个方法，用来获取restTemplate对象
    //将这个对象加入容器管理
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    @LoadBalanced
    @Bean
    public RestTemplate LoadBalanceRestTemplate(){
        return new RestTemplate();
    }

    @RestController
    public class ConsumerController{
        @Value("${spring.application.name}")
        private String appName;
        @Autowired
        private RestTemplate restTemplate;
        @Autowired
        private RestTemplate LoadBalanceRestTemplate;
        @Autowired
        private LoadBalancerClient loadBalancerClient;
        @Autowired
        private ConsumerHttpApi consumerHttpApi;
        @GetMapping("/consumer/doRestEcho")
        public String doRestEcho(){
            ServiceInstance serviceInstance =
                    loadBalancerClient.choose("nacos-provider");
            //服务提供方url
            /**
             * String API，format方法
             */
            String url = String.format("http://%s:%s/provider/echo/%s",
                    serviceInstance.getHost(),
                    serviceInstance.getPort(),
                    appName);
            System.out.println("request url:"+url);
            //URL和响应数据类型
            //向服务提供方发起请求
            return restTemplate.getForObject(url, String.class);
        }
        //注解版实现负载均衡的服务调用

        /**
         * 在RestTemplate对象构建时，通过@LoadBalance注解，高速Spring容器赋予RestTemplate
         * 对象负载均衡特性，在定义要访问的服务url时，直接通过服务名找到对应的服务实例进行访问
         * @return
         */
        @GetMapping("/consumer/doLoadBalanceRestEcho")
        public String doLoadBalanceRestEcho(){
            String url = String.format("http://nacos-provider/provider/echo/%s",
                    appName);
            //URL和响应数据类型
            //向服务提供方发起请求
            return LoadBalanceRestTemplate.getForObject(url, String.class);
        }

        @GetMapping("/consumer/echo/{msg}")
        public String echoMsg(@PathVariable String msg){
            return consumerHttpApi.echoMsg(msg);

        }
    }
}
